package com.jt.www.common.controller;

import com.jt.www.common.service.ProductVersionService;
import com.jt.www.domain.po.ProductVersion;
import com.jt.www.model.reps.GenericDataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 产品版本
 *
 * @author shaoyn
 * @create 2020年7月2日
 */
@Api(value = "产品版本维护", tags = {"PRODUCT-VERSION-API"}, description = "产品版本维护")
@RestController
@RequestMapping(value = "/product/version")
public class ProductVersionController {

    public static final Logger log = LoggerFactory.getLogger(ProductVersionController.class);

    @Autowired
    ProductVersionService productVersionService;

    /**
     * 通过产品编号,获得该别名下所有的产品编号
     * 以产编号为查询入参，查询别名一样的所有版本的产品编号，逗号分割
     *
     * @param productCode
     * @return
     */
    @ApiOperation(value = "通过产品编号,获得该别名下所有的产品编号", notes = "通过产品编号,获得该别名下所有的产品编号", httpMethod = "GET")
    @GetMapping(value = "/getAllProductCode/{productCode}")
    public ResponseEntity getAllProductCode(@ApiParam(value = "产品编号", required = true)
                                            @PathVariable("productCode") String productCode) {

        String productCodes = productVersionService.getAllProductCode(productCode);
        if (StringUtils.isNotBlank(productCodes)) {
            return GenericDataResponse.okWithData(productCodes);
        }
        //如果为空返回入参
        return GenericDataResponse.okWithData(productCode);
    }


    /**
     * 查询产品属性
     * 以产编号为查询入参，查询当前生效的(开始与结束时间内)版本的产品编号，逗号分割
     *
     * @param productCode
     * @return
     */
    @ApiOperation(value = "通过产品编号 获得该别名下所有的生效产品编号", notes = "通过产品编号 获得该别名下所有的生效产品编号", httpMethod = "GET")
    @GetMapping(value = "/getAllValidProductCode/{productCode}")
    public ResponseEntity getAllValidProductCode(@ApiParam(value = "产品编号", required = true)
                                                 @PathVariable("productCode") String productCode) {
        String productCodes = productVersionService.getAllValidProductCode(productCode);
        if (StringUtils.isNotBlank(productCodes)) {
            return GenericDataResponse.okWithData(productCodes);
        }
        //如果为空返回入参
        return GenericDataResponse.okWithData(productCode);
    }


    /**
     * 产品版本插入
     *
     * @return
     */
    @ApiOperation(value = "产品版本插入", notes = "产品版本插入", httpMethod = "POST")
    @PostMapping(value = "/saveProductVersion", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity saveProductVersion(@RequestBody ProductVersion productVersion) {
        if (StringUtils.isBlank(productVersion.getProductCode())) {
            return GenericDataResponse.errorWithMsg("产品编号不能为空!");
        }
        if (StringUtils.isBlank(productVersion.getProductAlias())) {
            return GenericDataResponse.errorWithMsg("产品别名不能为空!");
        }
        int n = productVersionService.saveProductVersion(productVersion);
        if (n > 0) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.errorWithMsg("保存失败!");
    }

    /**
     * 修改产品版本
     *
     * @return
     */
    @ApiOperation(value = "修改产品版本", notes = "修改产品版本", httpMethod = "POST")
    @PostMapping(value = "/updateProductVersion", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity updateProductVersion(@RequestBody ProductVersion productVersion) {
        if (productVersionService.updateProductVersion(productVersion) > 0) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.errorWithMsg("没有修改记录!");
    }
}
